package com.ruoyi.sl.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sl.domain.*;
import com.ruoyi.sl.mapper.*;
import com.ruoyi.sl.service.ISlWagesDayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sl.service.ISlDbinfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 待办Service业务层处理
 * 
 * @author admin
 * @date 2024-06-04
 */
@Service
public class SlDbinfoServiceImpl implements ISlDbinfoService 
{
    @Autowired
    private SlDbinfoMapper slDbinfoMapper;
    @Autowired
    private SlRworkMapper slRworkMapper;
    @Autowired
    private SlRfworkMapper slRfworkMapper;
    @Autowired
    private SlCheckConfigMapper slCheckConfigMapper;
    @Autowired
    private SlCheckUserMapper slCheckUserMapper;
    @Autowired
    private SlRfworkCheckMapper slRfworkCheckMapper;

    @Autowired
    private ISlWagesDayService slWagesDayService;

    /**
     * 查询待办
     * 
     * @param dbId 待办主键
     * @return 待办
     */
    @Override
    public SlDbinfo selectSlDbinfoByDbId(Long dbId)
    {
        return slDbinfoMapper.selectSlDbinfoByDbId(dbId);
    }

    /**
     * 查询待办列表
     * 
     * @param slDbinfo 待办
     * @return 待办
     */
    @Override
    public List<SlDbinfo> selectSlDbinfoList(SlDbinfo slDbinfo)
    {
        return slDbinfoMapper.selectSlDbinfoList(slDbinfo);
    }

    /**
     * 新增待办
     * 
     * @param slDbinfo 待办
     * @return 结果
     */
    @Override
    public int insertSlDbinfo(SlDbinfo slDbinfo)
    {
        return slDbinfoMapper.insertSlDbinfo(slDbinfo);
    }


    /**
     * 修改待办
     * 
     * @param slDbinfo 待办
     * @return 结果
     */
    @Override
    public int updateSlDbinfo(SlDbinfo slDbinfo)
    {
        return slDbinfoMapper.updateSlDbinfo(slDbinfo);
    }


    /**
     * 计件报工单审核通过
     *
     * @param dbId 待办ID
     * @return 结果
     */
    @Transactional
    @Override
    public int approvedDbinfo(Long dbId)
    {
        SlDbinfo slDbinfo = slDbinfoMapper.selectSlDbinfoByDbId(dbId);
        slDbinfo.setIsDb("1");  //转为已办
        slDbinfo.setIsDone("5");    //提交复核
        slDbinfo.setReviewsTime(DateUtils.getNowDate());    //审核通过时间
        slDbinfo.setDoneTime(DateUtils.getNowDate());   //办理时间
        int rows = slDbinfoMapper.updateSlDbinfo(slDbinfo);

        SlCheckConfig checkConfig = slCheckConfigMapper.selectSlCheckConfigByCheckcfgId(1L);
        if(StringUtils.isNotNull(checkConfig)){
            //给所有复核人创建待办
            SlDbinfo slCheckDbinfo = new SlDbinfo();
            slCheckDbinfo.setDbLevel(slDbinfo.getDbLevel());
            slCheckDbinfo.setDbSubject(slDbinfo.getDbSubject());
            slCheckDbinfo.setIsDb("0");  //待办
            slCheckDbinfo.setReceptionTime(DateUtils.getNowDate()); //接收时间
            slCheckDbinfo.setPresenterNick(slDbinfo.getTransactorsNick());    //提交人名称
            slCheckDbinfo.setPresenterUser(slDbinfo.getTransactorsUser());    //提交人ID
            slCheckDbinfo.setIsDone("5");   //状态
            slCheckDbinfo.setSourceId(slDbinfo.getSourceId());
            slCheckDbinfo.setSource(slDbinfo.getSource());
            slCheckDbinfo.setRwUser(slDbinfo.getRwUser());
            slCheckDbinfo.setRwDate(slDbinfo.getRwDate());
            List<SlCheckUser> checkUserList = checkConfig.getSlCheckUserList();
            for (SlCheckUser checkUser:checkUserList) {
                slCheckDbinfo.setTransactorsNick(checkUser.getCheckNick());  //处理人名
                slCheckDbinfo.setTransactorsUser(checkUser.getCheckUser());  //处理人ID
                slDbinfoMapper.insertSlDbinfo(slCheckDbinfo);
            }
        }



        //更新报工单状态
        SlRfwork slRfwork= new SlRfwork();
        slRfwork.setRfworkId(slDbinfo.getSourceId());
        slRfwork.setLeaderPass("0");
        slRfwork.setLeaderDate(DateUtils.getNowDate());
        slRfwork.setIsNew("1");
        slRfworkMapper.updateSlRfwork(slRfwork);

        return rows;
    }


    /**
     * 报工单复核通过
     *
     * @param dbId 待办ID
     * @return 结果
     */
    @Transactional
    @Override
    public int checkPassDbinfo(Long dbId){
        //待办变已办
        SlDbinfo slDbinfo = slDbinfoMapper.selectSlDbinfoByDbId(dbId);
        slDbinfo.setIsDb("1");  //转为已办
        slDbinfo.setReviewsTime(DateUtils.getNowDate());    //审核通过时间
        slDbinfo.setDoneTime(DateUtils.getNowDate());   //办理时间
        int rows = slDbinfoMapper.updateSlDbinfo(slDbinfo);

        //添加报工单复核人信息
        SlRfworkCheck slRfworkCheck = new SlRfworkCheck();
        slRfworkCheck.setDbId(slDbinfo.getDbId());
        slRfworkCheck.setRfworkId(slDbinfo.getSourceId());
        slRfworkCheck.setRfwordType(slDbinfo.getSource());
        slRfworkCheck.setCheckUser(slDbinfo.getTransactorsUser());
        slRfworkCheck.setCheckNick(slDbinfo.getTransactorsNick());

        SlCheckUser slCheck = slCheckUserMapper.selectSlCheckUserByUserName(slDbinfo.getTransactorsUser());
        slRfworkCheck.setIsHr(slCheck.getIsHr());
        slRfworkCheck.setCheckTime(DateUtils.getNowDate());
        slRfworkCheckMapper.insertSlRfworkCheck(slRfworkCheck);

        //复核规则配置
        SlCheckConfig checkConfig = slCheckConfigMapper.selectSlCheckConfigByCheckcfgId(1L);
        int checkUserCount = checkConfig.getUserCount();    //最低复核人数
        int containhrCount = checkConfig.getContainhrCount();   //至少包含几个HR
        //根据报工单ID获取报工单有几个复核人了
        List<SlRfworkCheck> slRfworkCheckList = slRfworkCheckMapper.selectSlRfworkCheckByRfworkId(slDbinfo.getSourceId());
        //最低几人，并且至少几个HR人员
        if(StringUtils.isNotEmpty(slRfworkCheckList)){
            if(slRfworkCheckList.size()>=checkUserCount){
                int ishr = 0;
                String checkPassUsers = "";
                String checkPassNicks = "";
                for (SlRfworkCheck checkUser:slRfworkCheckList) {
                    if("Y".equals(checkUser.getIsHr())){
                        ishr = ishr + 1;
                    }
                    if ("".equals(checkPassUsers)){
                        checkPassUsers = checkPassUsers;
                        checkPassNicks = checkPassNicks;
                    }else{
                        checkPassUsers = checkPassUsers + "," + checkUser.getCheckUser();
                        checkPassNicks = checkPassNicks + "," + checkUser.getCheckNick();
                    }
                }
                if(ishr>=containhrCount){
                    //报工单复核完成，结束所有相关待办
                    SlDbinfo overDbinfo = new SlDbinfo();
                    overDbinfo.setSourceId(slDbinfo.getSourceId());
                    List<SlDbinfo> overDbList = slDbinfoMapper.selectSlDbinfoList(overDbinfo);
                    if (StringUtils.isNotEmpty(overDbList)){
                        for (SlDbinfo dbinf:overDbList) {
                            dbinf.setIsDb("1");
                            dbinf.setCheckTime(DateUtils.getNowDate());
                            dbinf.setOverTime(DateUtils.getNowDate());
                            dbinf.setIsDone("6");
                            slDbinfoMapper.updateSlDbinfo(dbinf);
                        }
                    }
                    SlRfwork slRfwork = slRfworkMapper.selectSlRfworkByRfworkId(slDbinfo.getSourceId());
                    slRfwork.setRecheckPass("0");
                    slRfwork.setRecheckDate(DateUtils.getNowDate());
                    slRfwork.setRecheckUser(checkPassUsers);
                    slRfwork.setRecheckNick(checkPassNicks);
                    slRfworkMapper.updateSlRfwork(slRfwork);

                    //计算工资
                    if ("0".equals(slRfwork.getRfwordType())){
                        slWagesDayService.countJiJianWsDay(slRfwork);
                    }else if ("1".equals(slRfwork.getRfwordType())){
                        slWagesDayService.countLingGongWsDay(slRfwork);
                    }



                }
            }
        }

        return rows;
    }

    /**
     * 批量删除待办
     * 
     * @param dbIds 需要删除的待办主键
     * @return 结果
     */
    @Override
    public int deleteSlDbinfoByDbIds(Long[] dbIds)
    {
        return slDbinfoMapper.deleteSlDbinfoByDbIds(dbIds);
    }

    /**
     * 删除待办
     *
     * @param sourceId 报工单ID
     * @param source 报工单类型
     * @return 结果
     */
    @Override
    public int deleteSlDbinfoByRfworkId(Long sourceId, String source){
        return slDbinfoMapper.deleteSlDbinfoByRfworkId(sourceId,source);
    }

    /**
     * 删除待办信息
     * 
     * @param dbId 待办主键
     * @return 结果
     */
    @Override
    public int deleteSlDbinfoByDbId(Long dbId)
    {
        return slDbinfoMapper.deleteSlDbinfoByDbId(dbId);
    }
}
